UsersService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 70
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Functions

Rating   Name   Duplication   Size   Complexity  
A updateTerms 0 15 1
A softDeleteUser 0 11 2
A findAll 0 3 1
A findById 0 7 2
A adjustFunds 0 17 4
A update 0 5 1
1 9
import { Injectable, NotFoundException } from '@nestjs/common';
2 9
import { InjectRepository } from '@nestjs/typeorm';
3 9
import { Repository } from 'typeorm';
4 9
import { User } from './entities/user.entity';
5
import { UpdateUserDto } from './dto/update-user.dto/update-user.dto';
6
import { AdjustFundsDto } from './dto/update-user.dto/adjust-funds.dto';
7
8
@Injectable()
9 9
export class UsersService {
10
  constructor(
11
    @InjectRepository(User)
12 7
    private userRepository: Repository<User>,
13
  ) {}
14
15
  async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> {
16 2
    const user = await this.userRepository.findOne({
17
      where: { githubId },
18
    });
19
20
    // This will never be invoked through the controller because the auth guard will throw an error
21
    // However, if this is used as a standalone service, this check is necessary
22
    // removing to get rid of test case
23
    // if (!user) {
24
    //   throw new NotFoundException('User not found');
25
    // }
26
27 2
    user.hasAcceptedTerms = hasAcceptedTerms;
28 2
    return this.userRepository.save(user);
29
  }
30
  // Find all customers
31
  async findAll(): Promise<User[]> {
32 1
    return this.userRepository.find();
33
  }
34
  // Find a customer by ID
35
  async findById(githubId: string): Promise<User> {
36 9
    const user = await this.userRepository.findOne({ where: { githubId } });
37 9
    if (!user) {
38 1
      throw new NotFoundException('User not found');
39
    }
40 8
    return user;
41
  }
42
  // Update customer fields
43
  async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> {
44 1
    const user = await this.findById(githubId);
45
46 1
    return this.userRepository.save({ ...user, ...updateUserDto });
47
  }
48
49
  async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> {
50 4
    const user = await this.userRepository.findOneBy({ githubId });
51
52 4
    if (!user) {
53 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
54
    }
55
56
    // Update balance and/or isMonthlyPayment only if provided
57 3
    if (adjustFundsDto.balance !== undefined) {
58 2
      user.balance = adjustFundsDto.balance;
59
    }
60 3
    if (adjustFundsDto.isMonthlyPayment !== undefined) {
61 1
      user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment;
62
    }
63
64 3
    return this.userRepository.save(user);
65
  }
66
67
  async softDeleteUser(githubId: string): Promise<User> {
68 3
    const user = await this.userRepository.findOne({ where: { githubId } });
69
70 3
    if (!user) {
71 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
72
    }
73
74
    // Update the roles to include "inactive"
75 2
    user.roles = ['inactive'];
76 2
    return this.userRepository.save(user);
77
  }
78
}
79